package com.woniu.service.impl;

import com.woniu.dao.*;
import com.woniu.pojo.*;
import com.woniu.service.ActionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ActionServiceImpl implements ActionService {
    @Resource
    private ActionDao actionDao;
    @Resource
    private DiscussDao discussDao;
    @Resource
    private ActuserDao actuserDao;
    @Resource
    private UserroleDao userroleDao;
    @Resource
    private RoleDao roleDao;

    /**
     * 按发布时间
     * @return
     */
    @Override
    public List<Action> selectAll(PageBean pageBean,String apalce, String begin, String end) throws ParseException {
        ActionExample actionExample = new ActionExample();
        ActionExample.Criteria criteria = actionExample.createCriteria();
        if(apalce != null && !apalce.equals("") ){
            criteria.andApalceLike("%"+apalce+"%");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        if(begin != null && !begin.equals("")){
            Date newbegin = simpleDateFormat.parse(begin+" 00:00");
            criteria.andAtimeGreaterThanOrEqualTo(newbegin);
        }
        if(end != null && !end.equals("")){
            Date newend = simpleDateFormat.parse(end + " 24:00");
            criteria.andAtimeLessThanOrEqualTo(newend);
        }

        //分页查询
        long countRow= actionDao.countByExample(actionExample);
        pageBean.setCountRow((int)countRow);
        Integer countPage=pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);
        actionExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        actionExample.setLimit(pageBean.getPageSize());

        //按照发布日期倒序
        actionExample.setOrderByClause("atime DESC");
        List<Action> actions = actionDao.selectByExample(actionExample);
        System.out.println("ActionServiceImpl.selectAll");
        return actions;
    }

    /**
     * 获取一个活动详细信息
     * @param aid
     * @return
     */
    @Override
    public List<Discuss> selectOne(Integer aid) {
        DiscussExample discussExample = new DiscussExample();
        DiscussExample.Criteria criteria = discussExample.createCriteria();
        criteria.andAidEqualTo(aid);
        List<Discuss> discusses = discussDao.selectByExample(discussExample);
       if(discusses.size() == 0){
           //将活动的对象返回
           Action action = actionDao.selectByPrimaryKey(aid);
           Discuss discuss = new Discuss();
           discuss.setAction(action);
           discusses.add(discuss);
       }
        return discusses;
    }

    /**
     * 点赞
     * @param aid
     * @return
     */
    @Override
    public Integer good(Integer aid,Integer good,HttpSession session) {
        //1将点赞行为加到用户 点赞表中
        Actuser actuser = new Actuser();
        User loginUser = (User) session.getAttribute("loginUser");
        actuser.setUid(loginUser.getUid());
        actuser.setAid(aid);
        int i1 = actuserDao.insertSelective(actuser);
        if(i1<1){return -1;}
        //2进行点赞就行good+1操作
        Action action = new Action();
        action.setAid(aid);
        //防止活动没有点赞报错
        if(good == null){good = 0;}
        action.setGood(good+1);
        int i = actionDao.updateByPrimaryKeySelective(action);
        return i;
    }
    /**
     * 取消点赞
     * @param aid
     * @return
     */
    @Override
    public Integer bad(Integer aid,Integer good,HttpSession session) {
        //1杀出用户 点赞表中点赞记录
        User loginUser = (User) session.getAttribute("loginUser");
        ActuserExample actuserExample = new ActuserExample();
        ActuserExample.Criteria criteria = actuserExample.createCriteria();
        criteria.andUidEqualTo(loginUser.getUid());
        criteria.andAidEqualTo(aid);
        int i1 = actuserDao.deleteByExample(actuserExample);
        if(i1<1){return -1;}
        Action action = new Action();
        action.setAid(aid);
        action.setGood(good-1);
        int i = actionDao.updateByPrimaryKeySelective(action);
        return i;
    }

    /**
     * 注意后期设定
     * @param discuss
     * @return
     */
    @Override
    public Integer submit(Discuss discuss,HttpSession session) {
        User user = (User)session.getAttribute("loginUser");
        discuss.setDname(user.getUname());
        int i = discussDao.insertSelective(discuss);
        return i;
    }
    /**
     * 是否点赞判断
     * @param aid
     * @param session
     * @return
     */
    @Override
    public Integer isgood(Integer aid, HttpSession session) {
        //先查询当前用户对这个活动有没有点赞
        User user = (User)session.getAttribute("loginUser");
        ActuserExample actuserExample = new ActuserExample();
        ActuserExample.Criteria criteria = actuserExample.createCriteria();
        criteria.andUidEqualTo(user.getUid());
        criteria.andAidEqualTo(aid);
        List<Actuser> actusers = actuserDao.selectByExample(actuserExample);
        //一个活动一个人点赞只能一个，拿取集合第一个值
        if(actusers.size() > 0){
            return -1;
        }
        return 1;
    }

    /**
     * 是否为管理员验证
     * @param session
     * @return
     */
    @Override
    public Boolean isManager(HttpSession session) {
        //根据uid 查 rid
        User user = (User)session.getAttribute("loginUser");
        UserroleExample userroleExample = new UserroleExample();
        userroleExample.createCriteria().andUidEqualTo(user.getUid());
        List<Userrole> userroles = userroleDao.selectByExample(userroleExample);
        // 根据rids 和 rname 和 删除方式查询1存在
        for(int i = 0 ;i<userroles.size();i++){
            RoleExample roleExample = new RoleExample();
            RoleExample.Criteria criteria = roleExample.createCriteria();
            criteria.andRidEqualTo(userroles.get(i).getRid());
            criteria.andRnameEqualTo("2");
            criteria.andSoftdelEqualTo("1");
            List<Role> roles = roleDao.selectByExample(roleExample);
            if(roles.size()>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public int saveaction(Action action) {
        int i = actionDao.insertSelective(action);
        return i;
    }
}
